package erwiki.ui.app.parts.workflow;

import java.util.Collection;
import java.util.List;

import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import erwiki.api.rwc.RWC;
import erwiki.api.workflow.Decision;
import erwiki.api.workflow.DecisionQueue;
import erwiki.api.workflow.Workflow;
import erwiki.api.workflow.WorkflowManager;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;

public class WikiWorkflowView {

	@Inject
	ESelectionService selectionService;

	@Inject
	WorkflowManager workflowManager;

	private Table tableInbox;
	private TableViewer tableViewerInbox;
	private Table tableOutbox;
	private TableViewer tableViewerOutbox;

	/**
	 * Create contents of the part.
	 *
	 * @param parent
	 * @wbp.parser.entryPoint
	 */
	@PostConstruct
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));

		TabFolder tabFolder = new TabFolder(container, SWT.BOTTOM);

		TabItem tbtmInbox = new TabItem(tabFolder, SWT.NONE);
		tbtmInbox.setText("Inbox");
		createTableViewerInbox(tabFolder, tbtmInbox);

		TabItem tbtmOutbox = new TabItem(tabFolder, SWT.NONE);
		tbtmOutbox.setText("Outbox");
		createTableViewerOutbox(tabFolder, tbtmOutbox);
	}

	private void createTableViewerInbox(TabFolder tabFolder, TabItem tbtmInbox) {
		this.tableViewerInbox = new TableViewer(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
		tableInbox = tableViewerInbox.getTable();
		tableInbox.setHeaderVisible(true);
		tbtmInbox.setControl(tableInbox);

		TableViewerColumn tviColumn_id = new TableViewerColumn(tableViewerInbox, SWT.NONE);
		TableColumn tblclmnId = tviColumn_id.getColumn();
		tblclmnId.setWidth(100);
		tblclmnId.setText("Id");

		TableViewerColumn tviColumn_item = new TableViewerColumn(tableViewerInbox, SWT.NONE);
		TableColumn tblclmnItem = tviColumn_item.getColumn();
		tblclmnItem.setWidth(100);
		tblclmnItem.setText("Item");

		TableViewerColumn tviColumn_requester = new TableViewerColumn(tableViewerInbox, SWT.NONE);
		TableColumn tblclmnRequester = tviColumn_requester.getColumn();
		tblclmnRequester.setWidth(100);
		tblclmnRequester.setText("Requester");

		TableViewerColumn tviColumn_received = new TableViewerColumn(tableViewerInbox, SWT.NONE);
		TableColumn tblclmnReceiver = tviColumn_received.getColumn();
		tblclmnReceiver.setWidth(100);
		tblclmnReceiver.setText("Received");

		TableViewerColumn tviColumn_actions = new TableViewerColumn(tableViewerInbox, SWT.NONE);
		TableColumn tblclmnActions = tviColumn_actions.getColumn();
		tblclmnActions.setWidth(100);
		tblclmnActions.setText("Actions");

		tableViewerInbox.setContentProvider(new ArrayContentProvider());
		tableViewerInbox.setLabelProvider(new DecisionsLabelProvider());

		// Set input. Текущие decisions пользователя.
		DecisionQueue dq = workflowManager.getDecisionQueue();
		Collection<Decision> decision = dq.getActorDecisions(RWC.INSTANCE.getWikiSession());
		tableViewerInbox.setInput(decision);
	}

	private void createTableViewerOutbox(TabFolder tabFolder, TabItem tbtmOutbox) {
		this.tableViewerOutbox = new TableViewer(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
		tableOutbox = tableViewerOutbox.getTable();
		tableOutbox.setHeaderVisible(true);
		tbtmOutbox.setControl(tableOutbox);

		TableViewerColumn tvoColumn_id = new TableViewerColumn(tableViewerOutbox, SWT.NONE);
		TableColumn tblclmnId = tvoColumn_id.getColumn();
		tblclmnId.setWidth(100);
		tblclmnId.setText("Id");

		TableViewerColumn tvoColumn_item = new TableViewerColumn(tableViewerOutbox, SWT.NONE);
		TableColumn tblclmnItem = tvoColumn_item.getColumn();
		tblclmnItem.setWidth(100);
		tblclmnItem.setText("Item");

		TableViewerColumn tvoColumn_currentActor = new TableViewerColumn(tableViewerOutbox, SWT.NONE);
		TableColumn tblclmnCurrentActor = tvoColumn_currentActor.getColumn();
		tblclmnCurrentActor.setWidth(100);
		tblclmnCurrentActor.setText("Current actor");

		TableViewerColumn tvoColumn_received = new TableViewerColumn(tableViewerOutbox, SWT.NONE);
		TableColumn tblclmnReceived = tvoColumn_received.getColumn();
		tblclmnReceived.setWidth(100);
		tblclmnReceived.setText("Received");

		TableViewerColumn tvoColumn_actions = new TableViewerColumn(tableViewerOutbox, SWT.NONE);
		TableColumn tblclmnActions = tvoColumn_actions.getColumn();
		tblclmnActions.setWidth(100);
		tblclmnActions.setText("Actions");

		tableViewerOutbox.setContentProvider(new ArrayContentProvider());
		tableViewerOutbox.setLabelProvider(new WorkflowsLabelProvider());

		// Set input. Текущие workflows пользователя.
		List<Workflow> workflows = workflowManager.getOwnerWorkflows(RWC.INSTANCE.getWikiSession());
		tableViewerOutbox.setInput(workflows);
	}

}
